<?php


namespace generate\field;

class webuploaderFile extends Field
{
    public static $html = <<<EOF
    {js href="__ADMIN_PLUGINS__/webuploader/webuploader.js" /}
{js href="__ADMIN_PLUGINS__/webuploader-file/webuploader-file.js" /}
{css href="__ADMIN_PLUGINS__/webuploader-file/webuploader-file.css" /}

<div class="layui-form-item">
<label for="[FIELD_NAME]" class="layui-form-label">名称</label>
<div class="layui-input-inline">
    <div class="layui-upload-drag" id="upload-[FIELD_NAME]">
        <i class="layui-icon"></i>
        <p>点击上传，或将文件拖拽到此处</p>
    </div>
</div>
</div>

<div id="upload-list-[FIELD_NAME]" class="upload-list">
{if isset(\$data) && !empty(\$data.name)}
{foreach \$data.name as \$vo}
<div class="upload-item">
    <div class="file_info">
        <div class="fileName" title="{\$vo.name}">{\$vo.name}</div>
    </div>

    <div class="operation"><span class="btn-delete" title="取消上传"><i
                class="layui-icon layui-icon-close" aria-hidden="true"></i></span><span
            class="btn-play" title="开始" style="display: none;"> <i class="iconfont icon-play"
                aria-hidden="true"></i></span><span class="btn-pause" title="暂停"
            style="display: none;"><i class="layui-icon layui-icon-pause"
                aria-hidden="true"></i></span><span class="btn-retry" title="重试"
            style="display: none;"><i class="layui-icon layui-icon-refresh"
                aria-hidden="true"></i></span></div><input type="hidden" name="name[]"
        value="{\$vo|arr_encode}">
</div>
{/foreach}
{/if}
</div>
<script>
webuploaderFile({
    containerId: '#upload-[FIELD_NAME]',
    url: "{:url('SlicingUpload/webuploader')}",//上传路径
    uploadList: '#upload-list-[FIELD_NAME]',
    name: '[FIELD_NAME][]', // 列表提交的name
    fileSizeLimit: 1024 * 1024 * 1024 * 50, //验证文件总大小是否超出限制, 超出则不允许加入队列。
    fileSingleSizeLimit: 1024 * 1024  * 500 //验证单个文件大小是否超出限制, 超出则不允许加入队列。
})
</script>

\n
EOF;


    //多图上传模型内修改器与获取器
    public static $modelAttrCode = <<<EOF
                //[FORM_NAME]获取器
                public function get[FIELD_NAME]Attr(\$value)
                {
                    \$info = json_decode(\$value, true);
                    \$data = [];
                    foreach (\$info as \$vo) {
                        \$data[] = json_decode(\$vo, true);
                    }
                    return \$data;
                }

                //[FORM_NAME]修改器
                public function set[FIELD_NAME]Attr(\$value)
                {
                    foreach(\$value as \$key => \$vo){
                        if(\$vo == '' || empty(\$vo)){
                            unset(\$value[\$key]);
                        }
                    }
                    return json_encode(\$value);
                }

\n
EOF;


    public static $rules = [
        'required'   => '非空',
        'file_size'  => '文件大小限制',
        'file_image' => '图片类型',
        'regular'    => '自定义正则'
    ];


    public static function create($data)
    {
        $html = self::$html;
        $html = str_replace('[FORM_NAME]', $data['form_name'], $html);
        $html = str_replace('[FIELD_NAME]', $data['field_name'], $html);
        return $html;
    }
}
